#include <iostream>
#include <Timestamp.h>
#include <thread>
#include <AsyncLogger.h>
#include <sstream>
#include <Logger.h>
#include <memory>

int start = 1;

std::unique_ptr<AsyncLogger::AsyncLogger> g_logger(new AsyncLogger::AsyncLogger("./", "test", 1024 * 1024 * 1024, false));

void asyncLog(const char* logline, int len) {
	g_logger->append(logline, len);
}

void thread_func(int s) {
	int start_ = s;
	std::cout << "start: " << start_ << std::endl;
	for (int i = s; i < s + 10000; ++i) {
		// 打印不同level的日志
		LOG_INFO << std::chrono::system_clock::now().time_since_epoch().count() << ": 第" << i << "次 logfile\n";
		// LOG_DEBUG << std::chrono::system_clock::now().time_since_epoch().count() << ": 第" << i << "次 logfile\n";
		// LOG_WARN << std::chrono::system_clock::now().time_since_epoch().count() << ": 第" << i << "次 logfile\n";
		// LOG_ERROR << std::chrono::system_clock::now().time_since_epoch().count() << ": 第" << i << "次 logfile\n";
	}
}

int main(int argc, char* argv[]) {
	AsyncLogger::Logger::setLogLevel(AsyncLogger::LogLevel::TRACE);
	AsyncLogger::Logger::setOutput(asyncLog);
	std::string name = argv[0];
	g_logger->start();
	std::vector<std::thread> threads;
	auto before = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
	for (int i = 0; i < 10; ++i) {
		int s = start;
		threads.push_back(std::thread([=]() {
			thread_func(s);
			}));
		start += 10000;
	}
	for (int i = 0; i < 10; ++i) {
		threads[i].join();
	}
	auto after = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
	std::cout << "test time: " << (after - before).count() << std::endl;
}
